Questions
43 of 50
1What is a pseudo-class in CSS?
2How are pseudo-classes different from pseudo-elements?
3What is the syntax of a pseudo-class in CSS?
4Give some commonly used pseudo-classes.
5What does the :hover pseudo-class do?
6What’s the purpose of the :active pseudo-class?
7How does the :visited pseudo-class work for links?
8What is the :focus pseudo-class used for?
9What does the :checked pseudo-class do?
10How does the :disabled pseudo-class behave?
11What is the difference between :first-child and :first-of-type?
12How do :nth-child() and :nth-of-type() differ?
13How do you select every odd or even element using pseudo-classes?
14How can you style an element when it’s being hovered over along with its child?
15What does the :not() pseudo-class do?
16How can you use multiple pseudo-classes together?
17What is the difference between :link and :visited?
18What does the :target pseudo-class represent?
19How can you use :empty to detect empty elements?
20How does the :root pseudo-class differ from the html selector?
21What does the :valid and :invalid pseudo-class do?
22How can you use :required and :optional pseudo-classes in forms?
23What is the use of :in-range and :out-of-range?
24What does :read-only and :read-write do?
25How can you style an input field when it’s autofilled?
26How does the :focus-within pseudo-class work?
27What’s the difference between :focus and :focus-visible?
28What is the purpose of the :placeholder-shown pseudo-class?
29How can you style a checkbox when it is checked or unchecked?
30How can you style invalid form inputs without JavaScript?
31Can pseudo-classes be chained together? Give an example.
32What is the specificity of pseudo-classes compared to normal selectors?
33How can you use :not() effectively to exclude elements from styling?
34How does :has() pseudo-class work, and why is it powerful?
35Is :has() supported in all browsers?
36How does :is() differ from :where() in terms of specificity?
37How can you combine :is() or :where() with other selectors for cleaner code?
38What’s the difference between :nth-child() and :nth-last-child()?
39How can you style only the last element of a list using pseudo-classes?
40What does the :lang() pseudo-class do?
41How do you change a button’s color when hovered but not when disabled?
42How can you highlight the current section in a menu using :target?
43How can you style a form field differently when focused and valid?
44How do you style every third list item differently?
45How can you style alternate table rows without adding extra classes?
46How do you hide empty <p> tags using pseudo-classes?
47How can you style the parent when any child inside it is focused?
48How can you highlight a link only when it’s both focused and hovered?
49How can you style the first letter of only the first paragraph using pseudo-classes?
50How would you use :has() to select a <div> that contains an image?
43 / 50

How can you style a form field differently when focused and valid?

Styling Form Fields Using :focus and :valid in CSS

You can use the :focus and :valid pseudo-classes together to style form fields based on their state. The :focus pseudo-class applies styles when a field is active (clicked or selected), while :valid applies when the field’s input satisfies its validation rules.

Key Points About :focus and :valid
  1. 1

    :focus targets form fields that are currently active or being edited by the user.

  2. 2

    :valid targets fields whose values meet validation criteria, such as matching a pattern or not being empty when required.

  3. 3

    You can combine them as input:focus:valid to apply styles only when both conditions are true.

Example: Styling Focused and Valid Input Fields

In this example, when the user clicks the input field, it gets a blue border due to :focus. Once a valid email is entered, the border turns green. When both focused and valid, the input background changes to light green.

Best Practices
  1. 1

    Use :focus and :valid to give users clear visual feedback on form interactions.

  2. 2

    Combine pseudo-classes like :focus:invalid for error highlighting.

  3. 3

    Ensure validation rules (like type or pattern) are properly set in HTML.

  4. 4

    Maintain accessibility by ensuring color changes have sufficient contrast.

Difficulty: 3/10
Topics: CSS pseudo-classes, form validation styling, user experience feedback

Scenario Questions

0-2 years experience
  1. 1

    How would you make a text input turn green when it's valid and has focus, and red when it's invalid but focused?

  2. 2

    What happens if you forget to add the required attribute to a form field — will :valid still work?

2-5 years experience
  1. 1

    A user reports that our form fields don’t change color when they type — we’re using :valid, but it’s not working. What could be broken?

  2. 2

    We added focus styles to our form fields, but now they conflict with our design system’s focus ring. How do you resolve this without losing accessibility?

5-8 years experience
  1. 1

    We have a form with 20+ dynamic fields that validate asynchronously — how do you efficiently style them as valid/invalid without causing layout thrashing or performance issues?

  2. 2

    How would you design a reusable form component that supports custom validation states (e.g., 'pending', 'success', 'error') while still leveraging native :valid/:invalid for accessibility?

8+ years experience
  1. 1

    Our legacy form system uses inline styles and JS for validation feedback — how would you migrate to CSS-based :valid/:focus styling without breaking existing workflows or accessibility audits?

  2. 2

    You’re designing a cross-team form component library — how do you enforce consistent validation styling across 50+ products while accommodating different brand guidelines and compliance requirements?

Follow-up Questions

  • What if the form field is invalid but focused — how do you handle that?
  • How would you make sure these styles work for screen readers?
  • Why might :valid not trigger as expected in some cases?